exports.__esModule = true;

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = class extends think.BaseController {
    /**
     * 导出
     * 
     * @returns 
     */
    exportExcelAction() {
        var _this = this;

        return _asyncToGenerator(function* () {
            let aj = think.ajaxJson();
            const flag = Number(_this.get('flag')) || Number(_this.post('flag'));
            const AreaService = _this.service('overallmerit/AreaService');
            const OrgService = _this.service('overallmerit/OrgService');
            const JobService = _this.service('overallmerit/JobService');

            const xlsx = require('node-xlsx');
            const path = require('path');
            const fs = require('fs');
            let filename = 'com.xlsx';
            let filePath = 'static/upload/com.xlsx';
            let resData = null;
            switch (flag) {
                case 1:
                    const areaData = {
                        level: Number(_this.get('level')),
                        cycle: Number(_this.get('cycle')),
                        rangeDate: _this.get('rangeDate')
                    };
                    filename = 'QYPJ.xlsx';
                    filePath = 'static/upload/QYPJ.xlsx';
                    resData = yield AreaService.exportExcel(areaData);
                    break;
                case 2:
                    const orgData = {
                        cycle: Number(_this.get('cycle')),
                        rangeDate: _this.get('rangeDate'),
                        orgList: _this.get('orgList')
                    };
                    filename = 'BMPJ.xlsx';
                    filePath = 'static/upload/BMPJ.xlsx';
                    resData = yield OrgService.exportExcel(orgData);
                    break;
                case 3:
                    const jobData = {
                        post_code: _this.post('post_code'),
                        cycle: Number(_this.post('cycle')),
                        rangeDate: _this.post('rangeDate'),
                        rangeDate2: _this.post('rangeDate2'),
                        userList: _this.post('userList')
                    };
                    filename = 'GW.xlsx';
                    filePath = 'static/upload/GW.xlsx';
                    resData = yield JobService.exportExcel(jobData);
                    break;
                case 4:
                    const Data = {
                        cycle: Number(_this.get('cycle')),
                        rangeDate: _this.get('rangeDate'),
                        orgid: Number(_this.get('orgid')),
                        type: Number(_this.get('type'))
                    };
                    filename = 'AJLB.xlsx';
                    filePath = 'static/upload/AJLB.xlsx';
                    resData = yield OrgService.exportCaseExcel(Data);
                    break;
                default:
                    break;
            }
            if (resData && !resData.error) {
                var buffer = xlsx.build([{ name: "mySheetName", data: resData }]); // Returns a buffer
                //将文件内容插入新的文件中
                fs.writeFileSync('www/' + filePath, buffer, { 'flag': 'w' });
                aj.result = { filePath: filePath };
            } else {
                aj.success = false;
                aj.msg = '查询失败';
            }
            return _this.json(aj);
        })();
    }

};